package vy.org.app.page.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vy.dal.dao.VyUserLoginDAO;
import org.vy.dal.dao.VyUserLoginDO;

import vy.org.app.common.AppConstants;

public class LoginAction extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm actform, HttpServletRequest request, HttpServletResponse response) {
		try{
			LoginForm form = (LoginForm)actform;
			VyUserLoginDO vydo = (VyUserLoginDO)VyUserLoginDAO.findByUserAndPwd(form.getLoginId(), form.getLoginPwd());
			
			if(vydo == null || vydo.getVyid() == null){
				return mapping.findForward("failure");
			}else{
				HttpSession session = request.getSession();
				session.setAttribute(AppConstants.SESSION_USER_ID, vydo.getVyid());
				session.setAttribute(AppConstants.SESSION_USER_NAME, vydo.getUsername());
				
				return mapping.findForward("success");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return mapping.findForward("failure");
	}
}
